package com.gljx.web.controller.JournalController;

import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.JournalEntry.DepDayCompleteJournal;
import com.gljx.web.Entity.JournalEntry.DepDayCompleteJournal2;
import com.gljx.web.Entity.User;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.JournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author lcx
 * @Date 2017/12/27 0027 下午 1:47
 * 部门日招生完成情况统计表
 */
@Controller
@RequestMapping("/")
public class DepDayCompleteJournalController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private JournalService journalService;

    /**
     * @Author lcx
     * @Date 2017/12/18 0018 上午 9:42
     * 查询加油站进销存统计报表
     */
    @RequestMapping(value = "/chooseDepDayCompleteJournal/look.action")
    public String chooseDepDayCompleteJournal(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");                                   //获取已经登录用户数据
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
        Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
        cale.add(Calendar.MONTH, -1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        Date resultDate = cale.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String beginTime = sdf.format(resultDate);

        request.setAttribute("company", department);
        request.setAttribute("beginTime", beginTime);

        return "backHome/journaling/manageJournaling/DepDayCompleteJournaling/DepDayCompleteJournalingList";

    }

    /**
     * @Author lcx
     * @Date 2017/12/27 0027 上午 10:10
     * 查询部门日招生完成情况统计表
     */
    @RequestMapping(value = "/selDepDayCompleteJournal")
    @ResponseBody
    @Transactional
    public Map selDepDayCompleteJournal(HttpServletRequest request, @RequestParam(defaultValue = "", value = "cfoilspecName") String cfoilspecName,
                                        @RequestParam(defaultValue = "", value = "begindate") String begindate,
                                        @RequestParam(defaultValue = "", value = "begindate2") String begindate2,
                                        @RequestParam(defaultValue = "", value = "enddate") String enddate,
                                        @RequestParam(defaultValue = "", value = "state") String state) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");                                   //获取已经登录用户数据
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
        //1.查询
        Map returnmap = new HashMap();

        Map map = new HashMap();
        map.put("companyId", department.getFid());

        //查询这个月的 部门日招生完成情况统计表
        if ("0".equals(state)) {
            map.put("beginTime", begindate);
            map.put("endTime", TimeFormat.sumOneMonth(begindate));

            List<DepDayCompleteJournal> depDayCompleteJournals = journalService.selectDepAdmissions(map);

            //本年累计
            List<DepDayCompleteJournal> depDayCompleteJournals1 = journalService.selectDepAdmissionsAsYears(map);

            returnmap.put("depDayComplete", depDayCompleteJournals);
            //合计
            returnmap.put("depDayComplete1", depDayCompleteJournals1);
        }
        //查询部门月度招生完成情况统计表(详细 有人员)
        if ("1".equals(state)) {
            map.put("beginTime", begindate2);
            List<DepDayCompleteJournal2> depDayCompleteJournals = journalService.selectDepAdmissionAsDepTjr(map);

            returnmap.put("depDayComplete", depDayCompleteJournals);
        }
        //查询部门月度招生完成情况统计表(详细 有部门)
        if ("2".equals(state)) {
            map.put("beginTime", begindate2);
            List<DepDayCompleteJournal2> depDayCompleteJournals = journalService.selectDepAdmissionAsDep(map);

            returnmap.put("depDayComplete", depDayCompleteJournals);
        }
        returnmap.put("state", state);
        returnmap.put("gsName", department.getFnameL2());

        //查询期末结存

        return returnmap;
    }

}
